//
//  CommentViewController.m
//  Growth
//
//  Created by Hobo.Chen on 12/30/11.
//  Copyright 2011 MEETStudio. All rights reserved.
//

#import "CommentViewController.h"
#import "commentCell.h"

@implementation CommentViewController

@synthesize tableView;
@synthesize commentArray;
@synthesize  CommentType;

@synthesize delegate;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


- (id)initWithPost:(NSArray *)array{
    
	[super init];
    if (commentArray == nil) {
		commentArray = [[NSMutableArray alloc] init];
	}
    
    [commentArray addObjectsFromArray:array];
    
  	return self;
    
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//	[self loadData];  //加载评论信息
  
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[commentArray release];
    [CommentType release];
    [super dealloc];
}



#pragma mark UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	
	//return 1;
    return commentArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	
	   
    return 1;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
	
	NSMutableDictionary *dict=[commentArray objectAtIndex:section];
    
	
	static NSString *MyIdentifier = @"commentCell";
	// Try to retrieve from the table view a now-unused cell with the given identifier.
	commentCell *cell =(commentCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
    if (cell == nil) {
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"commentCell" 
                                                     owner:self options:nil];
#ifdef __IPHONE_2_1
		cell = (commentCell *)[nib objectAtIndex:0];
#else
		cell = (commentCell *)[nib objectAtIndex:1];
#endif
        
        
        cell.delegate = self;
        
	}
		
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell loadCellWith:dict  isSubComment:NO];
     
      
        
    return cell;
	
	
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	
	
	return nil;
}


#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	
	UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    CLog(@"cell height = %f",cell.frame.size.height);
	return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    int superId ;
    
    if (![[[commentArray objectAtIndex:indexPath.section] objectForKey:@"id"] isMemberOfClass:[NSNull class]]) {
        
        superId = [[[commentArray objectAtIndex:indexPath.section] objectForKey:@"id"] intValue];
    }
    else{
        superId = -1;
    }
        
    int commentTo ;
    if(indexPath.row>0){
        if (![[[[[commentArray objectAtIndex:indexPath.section] objectForKey:@"diaryComments"] objectAtIndex:indexPath.row-1] objectForKey:@"userId"] isMemberOfClass:[NSNull class]]) {
            
            commentTo = [[[[[commentArray objectAtIndex:indexPath.section] objectForKey:@"diaryComments"] objectAtIndex:indexPath.row-1] objectForKey:@"userId"] intValue];
            
        }
     
    
    }
    else{
        commentTo = -1;
    }
        if (self.delegate != nil) {
//        [self.delegate setCommentPrama:superId commentTo:commentTo];
//        [self.delegate addCommentAction:nil];
    }
    
}


#pragma mark commentCell delegate
- (IBAction)toUserView:(id)sender{
   
    UIButton *button = (UIButton *)sender;
    if (self.delegate!=nil) {
        [self.delegate toUserView:button];
    }
    
}

@end
